/**
 * 主页面控制JS
 */
var commonAPI = new OplkEms();
$(document).ready(function() {
	$('.nav-pills').html(domMenu);
	
	commonAPI.initAccordion();
	commonAPI.initTabs();
	commonAPI.initTree();

	initPopovers();
	commonAPI.initTable();
	commonAPI.initMenubar();
	commonAPI.initLeftNavHandler();
	commonAPI.initWindowEvent();
	commonAPI.initConnCheck();
	initGlobaTimer();
	initStatusBar();
	
	initDWR();
	
});

/**
 * 时钟计时器
 */

function initGlobaTimer(){
//	setInterval(function(){
//		testUpdateAlartToolbar();
//		testUpdateAlertTable();
//	}, 3000);

}

// =======================================Top Alart Toolbar Start
// =======================================//
/**
 * 顶端告警栏
 */
function testUpdateAlartToolbar() {
	$('#alertUrgent').html(Math.floor(Math.random() * 100));
	$('#alertMajor').html(Math.floor(Math.random() * 100));
	$('#alertMinor').html(Math.floor(Math.random() * 100));
	$('#alertInfo').html(Math.floor(Math.random() * 100));
};
// =======================================Top Alart Toolbar End
// =======================================//



/**
 * 初始化Popovers
 */
function initPopovers() {
	var options = {
		animation : true,
		trigger : 'hover' // 触发tooltip的事件
	};
	$("[id='alertInfo']").each(function(index, data) {
		$(data).popover(options);
	});
}

/**
 * 初始化状态栏事件
 */
function initStatusBar() {
	$('#chkWorkMode')
			.click(
					function() {
						var isSelected = $(this)[0].checked;
						if (isSelected) {
							commonAPI.previewMode();
							commonAPI
									.info('通知',
											'进入展示模式。<br> 点击底部<展示模式>复选框,退出模式，进入到工作模式。工作模式可以进行正常的操作。');
						} else {
							commonAPI.workMode();
							commonAPI.info('通知', '退出展示模式成功，进入工作模式。');
						}
					});
}

function openAlarmTab() {
	commonAPI.createTab('alarmCur', '当前告警同步',
			'/webbase/alarm/alarmcur');
}


function initDWR(){
	dwr.engine.setActiveReverseAjax(true);
	MessagePush.session2Server();
}

function parseMessage(data){
	//先对本页面进行调
	receiveMessage(data);
	//再对其它页面进行分析
	var frame = $('#tab_cont').find('iframe');
	for(var i=0; i<frame.length; i++){
		var document = frame[i].contentWindow;
		//子页面需要有receiveMessage方法来接收消息 
		if(document.receiveMessage){
			document.receiveMessage(data);
		}
	}
}

/**
 * 目前首页只能接收告警推送信息
 * @param data
 */
function receiveMessage(data){
	
	var module = data.module;
	var type = data.type;
	var action = data.action;
	
	if(action == 'on'){
		if(type == 'alarmNe'){
			addAlarm(data.entity);
		}
	}else if(action=='off'){
		if(type == 'alarmNe'){
			delAlarm(data.entity);
		}
	}
} 

function addAlarm(alarm){
	//1. 处理表格
	var level = alarm.severity.severity;
	//var atype = alarm.atype.alarmdesc;
	var aid = alarm.aid;
	var traptype = '';
	var sname = alarm.nenode.site.sname;
	var ip = alarm.nenode.ip;
	var slot = alarm.slot?alarm.slot.num:'';
	var card = alarm.slot?alarm.slot.card.cardname:'';
	var atime = alarm.atime;
	var color = 'grey';
	var colorArray = ['grey','red','orange','yellow','purple'];
	
	var levelid = alarm.severity.id;
	if(level>0){
		color = colorArray[levelid];
	}
	var str = '<tr alarmId="'+alarm.id+'">';
	str = str + '<td style="text-align: center;"><span class="label label-'+color+'">&nbsp;'+level+'&nbsp;&nbsp;</span></td>';
	str = str + '<td>'+aid+'</td>';
	str = str + '<td>'+traptype+'</td>';
	str = str + '<td>'+sname+'</td>';
	str = str + '<td>'+ip+'</td>';
	str = str + '<td>'+slot+'</td>';
	str = str + '<td>'+card+'</td>';
	str = str + '<td>'+atime+'</td>';
	str = str + '<td><a name="linkoper" style="cursor:hand;" onclick="commonAPI.createTab(\'21\',\'当前告警同步\',\'/webbase/alarm/alarmcur\');">more...</a></td>';
	str = str + '<td style="display:none;">'+alarm.nenode.site.sid+'_'+alarm.nenode.nid+'_'+slot+'</td>';
	str = str + '</tr>';
	
	  
	  var trs = $("#tbodyHtml").find('tr');
	  //删除最后一行
	  if(trs.length == 5){
		  trs[trs.length-1].remove();
	  }
	  //将新行添加在第一行 
	  $("#tbodyHtml")[0].insertBefore($(str)[0],trs[0]);
	  
	  //2. 处理Shortcut
	  var spans = $('#mainMenuBar').find('p').find('span');
	  
	  var cutIndex = alarm.severity.id-1;
	  if(cutIndex == 9){//要是nodeOffline告警的话，则加到最后一个index
		  cutIndex = 4;
	  }
	  
	  var text = spans.eq(cutIndex).text();
	  if(text==''){
		  text = '0';
	  }
	  spans.eq(cutIndex).text(parseInt(text)+1);
	  var d_c = spans.eq(cutIndex).attr('data-content');
	  var _d_c = d_c.replace(new RegExp('[0-9]{1,}'),parseInt(text)+1);
	  spans.eq(cutIndex).attr('data-content',_d_c);
}

function delAlarm(entity){
	//1. 处理表格，如果表格中有要删除的元素。则要删除该行
	 var trs = $("#tbodyHtml").find('tr');
	 for(var i=0; i<trs.length; i++){
		 var id = trs.eq(i).attr('alarmid');
		 if(entity.id == id){
			 trs[i].remove();
		 }
	 }
	
	//2. 处理Shortcut
	var spans = $('#mainMenuBar').find('p').find('span');
	  
	var cutIndex = entity.severity.id-1;
	if(cutIndex == 9){//要是nodeOffline告警的话，则加到最后一个index
		cutIndex = 4;
	}
	
	var text = spans.eq(cutIndex).text();
	if(text==''){
		text = '0';
	}
	spans.eq(cutIndex).text(parseInt(text) - 1);
	var d_c = spans.eq(cutIndex).attr('data-content');
	var _d_c = d_c.replace(new RegExp('[0-9]{1,}'),parseInt(text)+1);
	spans.eq(cutIndex).attr('data-content',_d_c);
	
}
